package com.market.config;

import com.market.config.realm.CustomRealm;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.LinkedHashMap;

/**
 * @author tan
 * @date 2022年12月30日 15:18
 * <p>
 * shiro视频代码，注释不全建议看参考代码
 */

@Configuration
public class ShiroConfiguration {

    @Bean
    public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //提供filter链    建立请求url和filter之间的映射关系
        //linked 保证map中的值有序
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        //key对应url value对应filter名称
        filterChainDefinitionMap.put("/admin/auth/login", "anon");
        filterChainDefinitionMap.put("/admin/auth/info", "anon");
        filterChainDefinitionMap.put("/wx/auth/login", "anon");

        // filterChainDefinitionMap.put("/admin/auth/noAuthc", "anon");
        filterChainDefinitionMap.put("/admin/user/list", "anon");
        // filterChainDefinitionMap.put("/admin/**", "anon");
//        filterChainDefinitionMap.put("/wx/**", "anon");
        //如果访问某个请求的时候，该请求对应的filter是authc，如果没有通过这个filter则说明没有权限
        // shiro会给你做重定向 → 默认的重定向的地址 /login.jsp
        // 如果想要修改重定向的地址，可以使用方法来修改
        // shiroFilterFactoryBean.setLoginUrl("/admin/auth/noAuthc");
//        filterChainDefinitionMap.put("/wx/address/list", "authc");
//        filterChainDefinitionMap.put("/wx/address/**", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;

    }

    @Bean
    public DefaultWebSecurityManager securityManager(CustomRealm realm, DefaultWebSessionManager sessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealms(Arrays.asList(realm));
        securityManager.setSessionManager(sessionManager);

        return securityManager;
    }

    //改用@Component注册组件
    // @Bean
    // public AuthorizingRealm realm() {
    //     return new CustomRealm();

    @Bean
    public DefaultWebSessionManager sessionManager() {

        // return new DefaultWebSessionManager();
        MarketSessionManager marketSessionManager = new MarketSessionManager();
        marketSessionManager.setGlobalSessionTimeout(999999999);
        return marketSessionManager;
    }


    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
